package com.zft.userwalletmanager.infrastructure.aop;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * 日志打印工具类
 */
@Component
@Aspect
@Slf4j
public class ParamInfoAspect {
    @Pointcut("execution(* com.zft.userwalletmanager.web.controller.*.*(..))")
    public void pointClass() {
    }

    @Around(value = "pointClass()")
    public Object printParam(ProceedingJoinPoint joinPoint) throws Throwable {
        // 开始时间
        long startTime = System.currentTimeMillis();

        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        Object[] args = joinPoint.getArgs();
        log.info("method [{}] , parameter:{}", method.getName(), JSON.toJSONString(args));
        Object proceed = joinPoint.proceed();
        log.info("method [{}] , result:{}, cost:{}ms", method.getName(), JSON.toJSONString(proceed), System.currentTimeMillis() - startTime);
        return proceed;
    }
}

